﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using CuaHangLaptop.BIZ;

namespace CuaHangLaptop.DAL
{
    class NhaSX_DAL
    {
        internal static int Get_Lasted_ID()
        {
            int id = 0;
            SqlConnection conn = Connection.Connection_String();
            SqlDataReader rdr = null;
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("select id from NhaSanXuat order by id desc", conn);
                rdr = cmd.ExecuteReader();
                rdr.Read();
                id = rdr.GetInt32(0);
            }
            finally
            {
                if (rdr != null) rdr.Close();
                conn.Close();
            }
            return id;
        }

        // SELECT * FROM NHA SAN XUAT
        internal static List<NhaSX_BIZ> GetAll_NSX()
        {
            List<NhaSX_BIZ> result = new List<NhaSX_BIZ>();
            SqlConnection conn = Connection.Connection_String();
            SqlDataReader rdr = null;
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("select * from NhaSanXuat", conn);
                rdr = cmd.ExecuteReader();

                while (rdr.Read())
                {
                    NhaSX_BIZ dd = new NhaSX_BIZ(rdr.GetInt32(0), rdr.GetString(1), rdr.GetString(2));
                    result.Add(dd);
                }
            }
            finally
            {
                if (rdr != null) rdr.Close();
                conn.Close();
            }
            return result;
        }

        // LẤY NHÀ SẢN XUẤT CÓ MANSX = NSX.MANSX
        internal static NhaSX_BIZ Get_NSX_ByCode(string _maNSX)
        {
            NhaSX_BIZ nhasx = new NhaSX_BIZ();
            SqlConnection conn = Connection.Connection_String();
            SqlDataReader rdr = null;
            try
            {
                conn.Open();
                String s = String.Format("select * from NhaSanXuat where MaNSX='{0}'", _maNSX);
                SqlCommand cmd = new SqlCommand(s, conn);
                rdr = cmd.ExecuteReader();
                rdr.Read();
                nhasx = new NhaSX_BIZ(rdr.GetInt32(0), rdr.GetString(1), rdr.GetString(2));
            }
            finally
            {
                if (rdr != null) rdr.Close();
                conn.Close();
            }
            return nhasx;
        }

        // DELETE
        internal static void Delete(NhaSX_BIZ nsx)
        {
            SqlConnection conn = Connection.Connection_String();
            try
            {
                conn.Open();
                String s = String.Format("delete from NhaSanXuat where id={0}", nsx.ID);
                SqlCommand cmd = new SqlCommand(s, conn);

                if (cmd.ExecuteNonQuery() > 0)
                {
                }
            }
            finally
            {
               conn.Close();
            }
        }

        // UPDATE
        internal static void Update(NhaSX_BIZ nsx)
        {
            SqlConnection conn = Connection.Connection_String();
            try
            {
                conn.Open();
                String s = String.Format("update NhaSanXuat set MaNSX='{0}', TenNSX='{1}' where id={2}", nsx.MaNSX, nsx.TenNSX, nsx.ID);
                SqlCommand cmd = new SqlCommand(s, conn);

                if (cmd.ExecuteNonQuery() > 0)
                {
                }
            }
            finally
            {
                conn.Close();
            }
        }

        // INSERT
        internal static int Insert(NhaSX_BIZ nsx)
        {
            SqlConnection conn = Connection.Connection_String();
            int newId = -1;
            SqlDataReader rdr = null;
            try
            {
                conn.Open();
                String s = String.Format("insert into Nhasanxuat values('{0}', '{1}')", nsx.MaNSX , nsx.TenNSX);
                SqlCommand cmd = new SqlCommand(s, conn);

                if (cmd.ExecuteNonQuery() > 0)
                {
                    s = String.Format("select Id from Nhasanxuat where MaNSX='{0}'", nsx.MaNSX);
                    cmd.CommandText = s;
                    rdr = cmd.ExecuteReader();

                    if (rdr.HasRows)
                    {
                        rdr.Read();
                        newId = Int32.Parse(rdr["Id"].ToString());
                    }
                }
            }
            finally
            {
                if (rdr != null) rdr.Close();
                conn.Close();
            }
            return newId;
        }

    }
}
